//
//  ThreeBtnCollectionViewCell.m
//  Food
//
//  Created by 15 on 15/7/17.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "ThreeBtnCollectionViewCell.h"
#import "BaseHeader.h"
#import "AFNet.h"
#import "ThreeTableViewCell.h"
#import "ThreeDownTableViewCell.h"
#import "ThreeDetailViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"

@implementation ThreeBtnCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(5, 15, SCREEN_WIDTH - 10, SCREEN_HEIGHT - 49 - 50 ) style:UITableViewStylePlain] autorelease];
        [self.contentView addSubview:self.tableView];
        self.arrFooter = [NSMutableArray array];
    } 
    return self;
}


- (void)setArrModel:(NSMutableArray *)arrModel
{
//    [self.arrModel removeAllObjects];
    if (_arrModel != arrModel) {
        [_arrModel release];
        _arrModel = [arrModel retain];
    }
    [self.tableView reloadData];
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.page = 2;
    self.contentView.backgroundColor = [UIColor whiteColor];

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 230;
    }
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrModel.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
        HealthKonw *model = self.arrModel[indexPath.row];
        [self.pushDelegate pushDelegateWith:model.IDCode titleName:model.title img:model.img];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if (indexPath.row  == 0) {
    ThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[[ThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
    }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        cell.backgroundColor =[UIColor redColor];
        HealthKonw *model = self.arrModel[0];
        cell.model = model;
    return cell;
    } else {
        ThreeDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[[ThreeDownTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell1"] autorelease];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.arrModel[indexPath.row];
        [self setUpReFresh];
        return cell;
    }
}
#pragma mark - 刷新与拉取
- (void)setUpReFresh
{
    // 上拉加载
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.tableView headerBeginRefreshing];
    // 设置文字
    self.tableView.headerPullToRefreshText = @"下拉刷新";
    self.tableView.headerReleaseToRefreshText = @"松开后刷新";
    self.tableView.headerRefreshingText = @"刷新中...";

    self.tableView.footerPullToRefreshText = @"上拉加载新数据";
    self.tableView.footerReleaseToRefreshText = @"松开后加载";
    self.tableView.footerRefreshingText = @"加载数据中...";
}
#pragma mark - 上拉加载
- (void)footerRereshing
{
    [self.arrFooter removeAllObjects];
    NSString *str1 = @"http://api.xiangha.com/zhishi5/getNousList?page=";
    NSString *str2 = @"&pinyin=";
    NSString *str4 = @"&type=classify&name=";
    NSString *str3 = [NSString stringWithFormat:@"%@%ld%@%@%@", str1, self.page++, str2, self.type, str4];
    [AFNet connectNetGetDataWithURLString:str3 parameters:nil finishblock:^(id result) {
        NSDictionary *dic = result[@"data"];
        NSArray *arr = dic[@"nous"];
        for (NSDictionary *d in arr) {
            HealthKonw *model = [[HealthKonw alloc] initWithDic:d];
            [self.arrFooter addObject:model];
        }
        [self.arrModel addObjectsFromArray:self.arrFooter];
        [self.tableView reloadData];
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
        [self.tableView footerEndRefreshing];
    });
}

@end
